package com.plum.admin.modules.sys.controller;


import com.plum.admin.common.annotation.Log;
import com.plum.admin.common.enums.LogType;
import com.plum.admin.common.util.R;
import com.plum.admin.modules.sys.entity.SysDept;
import com.plum.admin.modules.sys.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.plum.admin.base.BaseController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/sys/dept")
@Api(tags = "部门API")
@RequiredArgsConstructor
public class SysDeptController extends BaseController {

    private final ISysDeptService deptService;

    @GetMapping("/getDeptTreeData")
    @ApiOperation("获取部门tree")
    @Log(type = LogType.OPERATION,title = "查询部门树")
    public R<Object> getDeptTreeData(){
        return R.ok(deptService.getDeptTreeData());
    }

    @PostMapping
    @ApiOperation("保存部门")
    @PreAuthorize("hasAnyAuthority('sys:dept:insert')")
    @Log(type = LogType.OPERATION,title = "保存部门")
    public R<Object> saveDept(@RequestBody SysDept dept){
        return  R.ok(deptService.save(dept));
    }

    @PutMapping
    @ApiOperation("修改部门")
    @PreAuthorize("hasAnyAuthority('sys:dept:edit')")
    @Log(type = LogType.OPERATION,title = "修改部门")
    public R<Object> updateDept(@RequestBody SysDept dept){
        return  R.ok(deptService.updateById(dept));
    }

    @DeleteMapping
    @ApiOperation("删除部门")
    @PreAuthorize("hasAnyAuthority('sys:dept:delete')")
    @Log(type = LogType.OPERATION,title = "删除部门")
    public R<Object> deleteDept(String ids){
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(deptService.deleteDept(idList));
    }
}
